<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/webpage/include/taglib.jsp"%>
<html>
<head>
	<style type="text/css" >
		._citys { width: 450px; display: inline-block; border: 1px solid #63B8FF; padding: 5px; position: relative;background-color: #C4C4C4; }
		._citys span { color: #424242; height: 15px; width: 15px; line-height: 15px; text-align: center; border-radius: 3px; position: absolute; right: 10px; top: 10px; border: 1px solid #56b4f8; cursor: pointer; }
		._citys0 { width: 100%; height: 34px; display: inline-block; border-bottom: 2px solid #56b4f8; padding: 0; margin: 0; background-color: #C4C4C4;}
		._citys0 li { display: inline-block; line-height: 34px; font-size: 15px; color: #888; width: 80px; text-align: center; cursor: pointer; }
		.citySel { background-color: #56b4f8; color: #424242 !important; }
		._citys1 { width: 100%; display: inline-block; padding: 10px 0; background-color: #C4C4C4;}
		._citys1 a { width: 83px; height: 35px; display: inline-block; background-color:#3B3B3B; color: #EED2EE; margin-left: 6px; margin-top: 3px; line-height: 35px; text-align: center; cursor: pointer; font-size: 13px; overflow: hidden; }
		._citys1 a:hover { color: #3B3B3B; background-color: #E0E0E0; }
		.AreaS { background-color: #FFC0CB  !important; color: #BC8F8F !important; }
	</style>
	<title>收入管理</title>
	<meta name="decorator" content="ani"/>
	<style>
		#searchresult
		{
			width: 260px;
			position: absolute;
			z-index: 1;
			overflow: hidden;
			left: 140px;
			top: 71px;
			background: #E0E0E0;
			border-top: none;

		}
		#searchresult1
		{
			width: 260px;
			position: absolute;
			z-index: 1;
			overflow: hidden;
			left: 140px;
			top: 71px;
			background: #E0E0E0;
			border-top: none;

		}
		#searchresult2
		{
			width: 260px;
			position: absolute;
			z-index: 1;
			overflow: hidden;
			left: 140px;
			top: 71px;
			background: #E0E0E0;
			border-top: none;

		}

		.line
		{
			font-size: 15px;
			background: #E0E0E0;
			width: 140px;
			padding:0px;
		}
		.line1
		{
			font-size: 15px;
			background: #E0E0E0;
			width: 140px;
			padding:0px;
		}
		.hover
		{
			background: #007ab8;
			width: 140px;
			color: #fff;
		}
		.std
		{
			width: 150px;
		}
	</style>
	<script type="text/javascript">
		var validateForm;
		var $table; // 父页面table表格iD
		var $topIndex;//弹出窗口的 index
		function doSubmit(table, index){//回调函数，在编辑和保存动作时，供openDialog调用提交表单。
		  if(validateForm.form()){
			  $table = table;
			  $topIndex = index;
			  jp.loading();
			  $("#inputForm").submit();
			  return true;
		  }

		  return false;
		}

		$(document).ready(function() {
			validateForm = $("#inputForm").validate({
				submitHandler: function(form){
					jp.post("${ctx}/ap/income/save",$('#inputForm').serialize(),function(data){
						if(data.success){
	                    	$table.bootstrapTable('refresh');
	                    	jp.success(data.msg);
	                    	jp.close($topIndex);//关闭dialog

	                    }else{
            	  			jp.error(data.msg);
	                    }
					})
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}

			});
			
	        $('#date').datetimepicker({
				 format: "YYYY-MM-DD HH:00"
		    });

		});
		
		function addRow(list, idx, tpl, row){
			$(list).append(Mustache.render(tpl, {
				idx: idx, delBtn: true, row: row
			}));
			$(list+idx).find("select").each(function(){
				$(this).val($(this).attr("data-value"));
			});
			$(list+idx).find("input[type='checkbox'], input[type='radio']").each(function(){
				var ss = $(this).attr("data-value").split(',');
				for (var i=0; i<ss.length; i++){
					if($(this).val() == ss[i]){
						$(this).attr("checked","checked");
					}
				}
			});
			$(list+idx).find(".form_datetime").each(function(){
				 $(this).datetimepicker({
					 format: "YYYY-MM-DD HH:mm:ss"
			    });
			});
		}
		function delRow(obj, prefix){
			var id = $(prefix+"_id");
			var delFlag = $(prefix+"_delFlag");
			if (id.val() == ""){
				$(obj).parent().parent().remove();
			}else if(delFlag.val() == "0"){
				delFlag.val("1");
				$(obj).html("&divide;").attr("title", "撤销删除");
				$(obj).parent().parent().addClass("error");
			}else if(delFlag.val() == "1"){
				delFlag.val("0");
				$(obj).html("&times;").attr("title", "删除");
				$(obj).parent().parent().removeClass("error");
			}
		}
        function se(evt) {
            ChangeCoords(); //控制查询结果div坐标
            var k = window.event ? evt.keyCode : evt.which;
            //输入框的id为txt_search，这里监听输入框的keyup事件
            //不为空 && 不为上箭头或下箭头或回车
            if ($("#department").val() != "" && k != 38 && k != 40 && k != 13) {
                $.ajax({
                    type: 'post',
                    async: true, //同步执行，不然会有问题
                    dataType: "json",
                    url: "${ctx}/infor/department/getByName?name="+$("#department").val(),
                    //提交的页面/方法
                    error: function (msg) {//请求失败处理函数
                        alert("数据加载失败");
                    },
                    success: function (data) { //请求成功后处理函数。
						/*  var objData = eval("(" + data.userName + ")");   */
                        console.log(data);
                        if (data.length > 0) {
                            var layer = "";
                            layer = "<table id='aa1'>";
                            $.each(data, function (idx, item) {
                                layer += "<tr class='line' id='"+item.id+"'><td class='std1'>" + item.name+ "</td></tr>";
                            });
                            layer += "</table>";
                            //将结果添加到div中
                            $("#searchresult").empty();
                            $("#searchresult").append(layer);
                            $(".line:first").addClass("hover");
                            $("#searchresult").css("display", "");
                            //鼠标移动事件

                            $(".line").hover(function () {
                                $(".line").removeClass("hover");
                                $(this).addClass("hover");
                            }, function () {
                                $(this).removeClass("hover");
                                //$("#searchresult").css("display", "none");
                            });
                            //鼠标点击事件
                            $(".line").click(function () {
                                $("#department").val($(this).text());
								/* ($(this).attr("id"));*/
                                $("#searchresult").css("display", "none");
                            });
                        } else {
                            $("#searchresult").empty();
                            $("#searchresult").css("display", "none");
                        }
                    }
                });
            }
            else if (k == 38) {//上箭头
                $('#aa1 tr.hover').prev().addClass("hover");
                $('#aa1 tr.hover').next().removeClass("hover");
                $('#department').val($('#aa1 tr.hover').text());
            }
            else if (k == 40) {//下箭头
                $('#aa1 tr.hover').next().addClass("hover");
                $('#aa1 tr.hover').prev().removeClass("hover");
                $('#department').val($('#aa1 tr.hover').text());
            }
            else if (k == 13) {//回车
                $('#department').val($('#aa1 tr.hover').text());
                ($('#aa1 tr.hover').attr("id"));
                $("#searchresult").empty();
                $("#searchresult").css("display", "none");
            }
            else {
                $("#searchresult").empty();
                $("#searchresult").css("display", "none");
            }
        };
        $("#searchresult").bind("mouseleave", function () {
            $("#searchresult").empty();
            $("#searchresult").css("display", "none");
        });
        function ChangeCoords() {
            //    var left = $("#txt_search")[0].offsetLeft; //获取距离最左端的距离，像素，整型
            //    var top = $("#txt_search")[0].offsetTop + 26; //获取距离最顶端的距离，像素，整型（20为搜索输入框的高度）
            var left = $("#department").position().left; //获取距离最左端的距离，像素，整型
            var top = $("#department").position().top + 30; ; //获取距离最顶端的距离，像素，整型（20为搜索输入框的高度）
            $("#searchresult").css("left", left + "px"); //重新定义CSS属性
            $("#searchresult").css("top", top + "px"); //同上
        }

        function em(evt) {
            ChangeCoords1(); //控制查询结果div坐标
            var k = window.event ? evt.keyCode : evt.which;
            //输入框的id为txt_search，这里监听输入框的keyup事件
            //不为空 && 不为上箭头或下箭头或回车
            if ($("#responsiblePerson").val() != "" && k != 38 && k != 40 && k != 13) {
                $.ajax({
                    type: 'post',
                    async: true, //同步执行，不然会有问题
                    dataType: "json",
                    url: "${ctx}/infor/staff/getByStaff?name="+$("#responsiblePerson").val(),
                    //提交的页面/方法
                    error: function (msg) {//请求失败处理函数
                        alert("数据加载失败");
                    },
                    success: function (data) { //请求成功后处理函数。
						/*  var objData = eval("(" + data.userName + ")");   */
                        console.log(data);
                        if (data.length > 0) {
                            var layer = "";
                            layer = "<table id='aa1'>";
                            $.each(data, function (idx, item) {
                                layer += "<tr class='line1' id='"+item.id+"'><td class='std1'>" + item.name+ "</td></tr>";
                            });
                            layer += "</table>";
                            //将结果添加到div中
                            $("#searchresult1").empty();
                            $("#searchresult1").append(layer);
                            $(".line1:first").addClass("hover");
                            $("#searchresult1").css("display", "");
                            //鼠标移动事件

                            $(".line1").hover(function () {
                                $(".line1").removeClass("hover");
                                $(this).addClass("hover");
                            }, function () {
                                $(this).removeClass("hover");
                                //$("#searchresult").css("display", "none");
                            });
                            //鼠标点击事件
                            $(".line1").click(function () {
                                $("#responsiblePerson").val($(this).text());
								/* ($(this).attr("id"));*/
                                $("#searchresult1").css("display", "none");
                            });
                        } else {
                            $("#searchresult1").empty();
                            $("#searchresult1").css("display", "none");
                        }
                    }
                });
            }
            else if (k == 38) {//上箭头
                $('#aa1 tr.hover').prev().addClass("hover");
                $('#aa1 tr.hover').next().removeClass("hover");
                $('#responsiblePerson').val($('#aa1 tr.hover').text());
            }
            else if (k == 40) {//下箭头
                $('#aa1 tr.hover').next().addClass("hover");
                $('#aa1 tr.hover').prev().removeClass("hover");
                $('#responsiblePerson').val($('#aa1 tr.hover').text());
            }
            else if (k == 13) {//回车
                $('#responsiblePerson').val($('#aa1 tr.hover').text());
                ($('#aa1 tr.hover').attr("id"));
                $("#searchresult1").empty();
                $("#searchresult1").css("display", "none");
            }
            else {
                $("#searchresult1").empty();
                $("#searchresult1").css("display", "none");
            }
        };
        $("#searchresult1").bind("mouseleave", function () {
            $("#searchresult1").empty();
            $("#searchresult1").css("display", "none");
        });
        function ChangeCoords1() {
            //    var left = $("#txt_search")[0].offsetLeft; //获取距离最左端的距离，像素，整型
            //    var top = $("#txt_search")[0].offsetTop + 26; //获取距离最顶端的距离，像素，整型（20为搜索输入框的高度）
            var left = $("#responsiblePerson").position().left; //获取距离最左端的距离，像素，整型
            var top = $("#responsiblePerson").position().top + 30; ; //获取距离最顶端的距离，像素，整型（20为搜索输入框的高度）
            $("#searchresult1").css("left", left + "px"); //重新定义CSS属性
            $("#searchresult1").css("top", top + "px"); //同上
        }
function js(val,idx) {
           var price=$("#earningsDetailList"+idx+"_price").val();
           var a=parseFloat(price)*parseFloat(val);
           $("#earningsDetailList"+idx+"_total").val(a);

}
	</script>
</head>
<body class="bg-white">
		<form:form id="inputForm" modelAttribute="income" action="${ctx}/ap/income/save" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<sys:message content="${message}"/>
			<input type="hidden" name="projectName" value="${income.projectName}"/>
		<table class="table table-bordered">
		   <tbody>
				<tr>
					<td class="width-15 active"><label class="pull-right">项目：</label></td>
					<td class="width-35">
						<sys:gridselect url="${ctx}/infor/project/data" id="projectName" name="projectName.id" value="${income.projectName.id}" labelName="projectName.name" labelValue="${income.projectName.name}"
										title="选择项目" cssClass="form-control required" fieldLabels="名称" fieldKeys="name" searchLabels="名称" searchKeys="name" ></sys:gridselect>
					</td>
					<td class="width-15 active"><label class="pull-right">经办人：</label></td>
					<td class="width-35">
						<sys:gridselect url="${ctx}/infor/staff/data" id="responsiblePerson" name="responsiblePerson.id" value="${income.responsiblePerson.id}" labelName="responsiblePerson.name" labelValue="${income.responsiblePerson.name}"
										title="选择人员名称" cssClass="form-control required" fieldLabels="名称" fieldKeys="name" searchLabels="名称" searchKeys="name" ></sys:gridselect>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">费用对象：</label></td>
					<td class="width-35">
						<form:select path="costObject" class="form-control ">
							<form:options items="${fns:getDictList('object')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
						</form:select>
					</td>

					<td class="width-15 active"><label class="pull-right">科目：</label></td>
					<td class="width-35">
							<form:input path="oneSubject" htmlEscape="false" id="city" class="form-control "/>
						<script>
                            $("#city").click(function (e) {
                                var province;
                                var area;
                                $.ajax({
                                    url:'${ctx}/ap/subject/getJson',
                                    async: false,
                                    success:function (data) {
                                        alert(data);
                                        province=data;
                                    }
                                });
                                <%--$.ajax({--%>
                                    <%--url:'${ctx}/ap/subject/getJson1',--%>
                                    <%--async: false,--%>
                                    <%--success:function (data) {--%>
                                        <%--area=data;--%>
                                    <%--}--%>
                                <%--});--%>
                                SelCity(this,e,province,area);
                            });
						</script>

					</td>
				</tr>

				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">发生日期：</label></td>
					<td class="width-35">
						<p class="input-group">
						<div class='input-group form_datetime' id='date'>
							<input type='text'  name="date" class="form-control "  value="<fmt:formatDate value="${income.date}" pattern="yyyy-MM-dd HH:mm:ss"/>"/>
							<span class="input-group-addon">
			                        <span class="glyphicon glyphicon-calendar"></span>
			                    </span>
						</div>
						</p>
					</td>
					<td class="width-15 active"><label class="pull-right">费用归属：</label></td>
					<td class="width-35">
						<form:select path="costAffiliation" class="form-control ">
							<form:options items="${fns:getDictList('guishu')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
						</form:select>
					</td>

				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">单据号：</label></td>
					<td class="width-35">
						<form:input path="documentNumber" htmlEscape="false"    class="form-control "/>
					</td>
					<td class="width-15 active"><label class="pull-right">票据类型：</label></td>
					<td class="width-35">
						<form:select path="billType" class="form-control ">
							<form:options items="${fns:getDictList('invoice_type')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
						</form:select>

					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">票据号码：</label></td>
					<td class="width-35">
						<form:input path="billNumber" htmlEscape="false"    class="form-control "/>
					</td>
					<td class="width-15 active"><label class="pull-right">票据上传：</label></td>
					<td class="width-35">
						<form:hidden id="billUploading" path="billUploading" htmlEscape="false" maxlength="500" class="form-control"/>
						<sys:ckfinder input="billUploading" type="files" uploadPath="/ap/income" selectMultiple="true" />
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">交款对象：</label></td>
					<td class="width-35">
						<form:input path="paymentObject" htmlEscape="false"    class="form-control "/>
					</td>
					<td class="width-15 active"><label class="pull-right">账户：</label></td>
					<td class="width-35">
						<form:input path="account" htmlEscape="false"    class="form-control "/>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">单据状态：</label></td>
					<td class="width-35">
						<form:input readonly="true" path="documentStatus" htmlEscape="false"     class="form-control "/>
					</td>
					<td class="width-15 active"><label class="pull-right">备注信息：</label></td>
					<td class="width-35">
						<form:input path="remarks" htmlEscape="false"    class="form-control "/>
					</td>
				</tr>
		 	</tbody>
		</table>
		<div class="tabs-container">
            <ul class="nav nav-tabs">
				<li class="active"><a data-toggle="tab" href="#tab-1" aria-expanded="true">收入明细：</a>
                </li>
            </ul>
            <div class="tab-content">
				<div id="tab-1" class="tab-pane fade in  active">
			<a class="btn btn-white btn-sm" onclick="addRow('#earningsDetailList', earningsDetailRowIdx, earningsDetailTpl);earningsDetailRowIdx = earningsDetailRowIdx + 1;" title="新增"><i class="fa fa-plus"></i> 新增</a>
			<table class="table table-striped table-bordered table-condensed">
				<thead>
					<tr>
						<th class="hide"></th>
						<th><span style="color: red">*</span>名称</th>
						<th>品牌</th>
						<th>型号</th>
						<th><span style="color: red">*</span>单价</th>
						<th><span style="color: red">*</span>数量</th>
						<th><span style="color: red">*</span>合计</th>
						<th>备注信息</th>
						<th width="10">&nbsp;</th>
					</tr>
				</thead>
				<tbody id="earningsDetailList">
				</tbody>
			</table>
			<script type="text/template" id="earningsDetailTpl">//<!--
				<tr id="earningsDetailList{{idx}}">
					<td class="hide">
						<input id="earningsDetailList{{idx}}_id" name="earningsDetailList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
						<input id="earningsDetailList{{idx}}_delFlag" name="earningsDetailList[{{idx}}].delFlag" type="hidden" value="0"/>
					</td>
					
					<td>
						<input id="earningsDetailList{{idx}}_name" name="earningsDetailList[{{idx}}].name" type="text" value="{{row.name}}"    class="form-control  required"/>
					</td>
					<td>
						<input id="earningsDetailList{{idx}}_brand" name="earningsDetailList[{{idx}}].brand" type="text" value="{{row.brand}}"    class="form-control "/>
					</td>
					<td>
						<input id="earningsDetailList{{idx}}_type" name="earningsDetailList[{{idx}}].type" type="text" value="{{row.type}}"    class="form-control "/>
					</td>
					
					<td>
						<input id="earningsDetailList{{idx}}_price" name="earningsDetailList[{{idx}}].price" type="text" value="{{row.price}}"    class="form-control required number"/>
					</td>
					
					
					<td>
						<input id="earningsDetailList{{idx}}_number" name="earningsDetailList[{{idx}}].number"   onblur="js(this.value,'{{idx}}')" type="text" value="{{row.number}}"    class="form-control required number"/>
					</td>
					
					
					<td>
						<input id="earningsDetailList{{idx}}_total" name="earningsDetailList[{{idx}}].total" type="text" value="{{row.total}}"    class="form-control required number"/>
					</td>
					
					
					<td>
						<input id="earningsDetailList{{idx}}_remarks" name="earningsDetailList[{{idx}}].remarks"    value="{{row.remarks}}"  class="form-control "/>
					</td>
					
					<td class="text-center" width="10">
						{{#delBtn}}<span class="close" onclick="delRow(this, '#earningsDetailList{{idx}}')" title="删除">&times;</span>{{/delBtn}}
					</td>
				</tr>//-->
			</script>
			<script type="text/javascript">
				var earningsDetailRowIdx = 0, earningsDetailTpl = $("#earningsDetailTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
				$(document).ready(function() {
					var data = ${fns:toJson(income.earningsDetailList)};
					for (var i=0; i<data.length; i++){
						addRow('#earningsDetailList', earningsDetailRowIdx, earningsDetailTpl, data[i]);
						earningsDetailRowIdx = earningsDetailRowIdx + 1;
					}
				});
			</script>
			</div>
		</div>
		</div>
		</form:form>
</body>
</html>